<template>
  <div class="register-container">
    <h2>注册</h2>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label>学号</label>
        <input v-model="form.stuNum" type="text" required />
      </div>
      <div class="form-group">
        <label>用户名</label>
        <input v-model="form.username" type="text" required />
      </div>
      <div class="form-group">
        <label>密码</label>
        <input v-model="form.pwd" type="password" required />
      </div>
      <div class="form-group">
        <label>真实姓名</label>
        <input v-model="form.realName" type="text" required />
      </div>
      <div class="form-group">
        <label>班级</label>
        <input v-model="form.className" type="text" required />
      </div>
      <div class="form-group">
        <label>邮箱</label>
        <input v-model="form.email" type="email" />
      </div>
      <div class="form-group">
        <label>年龄</label>
        <input v-model="form.age" type="number" />
      </div>
      <button type="submit">注册</button>
    </form>
  </div>
</template>

<script>
// src/views/Auth/Register.vue
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/stores/auth';

export default {
  setup() {
    const router = useRouter();
    const authStore = useAuthStore();

    const form = ref({
      stuNum: '', 
      username: '',
      pwd: '',
      realName: '',
      className: '23软件2班',
      email: '@qq.com',
      age: 18
    });

    const handleRegister = async () => {
  try {
    // 验证必填字段
    if (!form.value.stuNum || !form.value.username || !form.value.pwd) {
      throw new Error('学号、用户名和密码为必填项');
    }

    await authStore.registerUser(form.value);
    alert('注册成功！');
    router.push('/login');
  } catch (error) {
    console.error('注册错误详情:', error);
    alert(`注册失败: ${error.message || '请检查输入信息'}`);
  }
};

    return { form, handleRegister };
  }
};
</script>

<style scoped>
.register-container {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 15px;
}
.form-group label {
  display: block;
  margin-bottom: 5px;
}
.form-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}
</style>